<template>
  <div class="chartNum">
    <div class="box-item">
      <div v-for="(item,index) in orderNum" :key="index">
        <li v-if="!isNaN(item)" class="number-item">
          <span>
            <i ref="numberItem">0123456789</i>
          </span>
        </li>
        <li v-else class="mark-item">
          {{item}}
        </li>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    numbers: {
      type: Number
    }
  },
  data() {
    return {
      orderNum: [] // 默认订单总数
    };
  },
  watch: {
    numbers: {
      handler(val) {
        console.log('val')
        console.log(val)
        if(val) {
          this.toOrderNum(this.numbers); // 这里输入数字即可调用
        }
      }
    }
  },
  mounted() {
    // setTimeout(() => {}, 100);
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      let qNum = this.formatQ(this.numbers);
      console.log(qNum);
      this.orderNum = qNum.toString().split("");
      this.$nextTick(() => {
        const numberItems = this.$refs.numberItem; // 拿到数字的ref，计算元素数量
        const numberArr = this.orderNum.filter(item => !isNaN(item));
        // 结合CSS 对数字字符进行滚动,显示订单数量
        for (let index = 0; index < numberItems.length; index++) {
          const elem = numberItems[index];
          elem.style.transform = `translate(0.06rem, -${numberArr[index] * 10 +
            1}%)`;
        }
      });
    },
    // 千分符
    formatQ(str) {
      if (!str) return;
      str = parseInt(str)
        .toString()
        .split(".");
      // console.log("第一次处理后的num",str);
      str[0] = str[0].replace(new RegExp("(\\d)(?=(\\d{3})+$)", "ig"), "$1,");
      // console.log("第一次处理后的num",str[0]);
      return str.join(".");
    },

    // 处理总订单数字
    toOrderNum(num) {
      console.log('num')
      console.log(num)
      num = num.toString();
      // 把订单数变成字符串
      if (num.length < 10) {
        num = "0" + num; // 如未满八位数，添加"0"补位
        this.toOrderNum(num); // 递归添加"0"补位
      } else if (num.length === 10) {
        // 订单数中加入逗号
        // num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
        this.orderNum = num.split(""); // 将其便变成数据，渲染至滚动数组
      }
      this.setNumberTransform();
    }
  }
};
</script>
<style scoped lang='less'>
/*订单总量滚动数字设置*/
.box-item {
  position: relative;
  // height: 100px;
  font-size: 0.6rem;
  line-height: 0.66rem;
  text-align: center;
  list-style: none;
  color: #ffd666;
  display: flex;
  justify-content: center;
}
/* 默认逗号设置 */
.mark-item {
  //width: 0.23rem;
  margin-right: 0.00rem;
  font-size: 0.3rem;
  font-family: DINAlternate-Bold, DINAlternate;
  position: relative;
  margin-top: -0.10rem;
}
/*滚动数字设置*/
.number-item {
  width: 0.34rem;
  height: 0.42rem;
 // border-radius: 0.04rem;
 // border: 0.01rem solid rgba(255, 214, 102, 1);
  /* 背景图片 */
 // background-size: 100% 100%;
  // background: #ccc;
  list-style: none;
  //margin-right: 0.00rem;
  // background:rgba(250,250,250,1);
 // border-radius: 4px;
  & > span {
    position: relative;
    display: inline-block;
    // margin-right: 10px;
    // right: 20%;
    // transform: translateX(32%);
    width: 100%;
    height: 100%;
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow-y: hidden;
	overflow-x:hidden;
    & > i {
      font-style: normal;
      position: absolute;
      top: 0.03rem;
      // left: 50%;
      // transform: translate(-55%, 0);
      transition: transform 1s ease-in-out;
      // letter-spacing: 10px;
    }
  }
}
</style>